package logic

import (
	"context"
	"errors"
	"hzjt-oam/rpc/sys/internal/config"
	"hzjt-oam/rpc/sys/internal/svc"
	"hzjt-oam/rpc/sys/model"
	"hzjt-oam/rpc/sys/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type ReSetPasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewReSetPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReSetPasswordLogic {
	return &ReSetPasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ReSetPasswordLogic) ReSetPassword(in *pb.ReSetPasswordReq) (*pb.ReSetPasswordResp, error) {
	err := l.svcCtx.UserModel.Update(l.ctx, &model.SysUser{
		Id:           in.Id,
		LastUpdateBy: in.LastUpdateBy,
		Password:     config.Md5(in.Password),
	})
	if err != nil {
		err = errors.New("密码重置失败")
		return nil, err
	}

	return &pb.ReSetPasswordResp{}, nil
}
